package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.comm.BaseController;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.service.WarnMessService;
import cn.nawang.ebeim.api.utils.Constant;

/**
 * 
 * @ClassName: WarnMessController
 * @Description: 预警事件控制器
 * @author huanghj
 * @date 2015年10月27日 下午4:38:45
 * @version v1.0
 */
@RestController
public class WarnMessController extends BaseController {
  private static final Logger LOG = LoggerFactory.getLogger(WarnMessController.class);
  @Autowired
  private WarnMessService warnMessService;

  @RequestMapping(value = "/token/warnMess/find/userId/pages", method = RequestMethod.GET)
  public Map<String, Object> findByUserIdByPage(Pages page, HttpServletRequest request) {
    LOG.debug("/token/warnMess/find/userId/pages {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 预警用户
    String userId = request.getParameter("userId");
    if (!StringUtils.isEmpty(userId)) {
      dtovect.add(new SearchFormParams("a.userId", userId, "userId", SearchFormParams.STRING_TYPE));
    }
    // 预警事件类型
    String warnType = request.getParameter("warnType");
    if (!StringUtils.isEmpty(warnType)) {
      dtovect.add(
          new SearchFormParams("a.warnType", warnType, "warnType", SearchFormParams.INTEGER_TYPE));
    }
    // 预警事件等级
    String warnLevel = request.getParameter("warnLevel");
    if (!StringUtils.isEmpty(warnLevel)) {
      dtovect.add(new SearchFormParams("a.warnLevel", warnLevel, "warnLevel",
          SearchFormParams.INTEGER_TYPE));
    }
    // 预警事件事件
    String warnTime = request.getParameter("warnTime");
    if (!StringUtils.isEmpty(warnTime)) {
      dtovect.add(
          new SearchFormParams("a.warnTime", warnTime, "warnTime", SearchFormParams.STRING_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = warnMessService.findAllByPaging(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findByUserIdByPage {}", map);
    return map;
  }
 
  @RequestMapping(value = "/token/warnMess/find/warnList", method = RequestMethod.GET)
  public Map<String, Object> findByUserIdList(String id,String type,String level,String begin,String end) {
    Map<String, Object> map = new HashMap<String, Object>();
    List<Map> list=warnMessService.getWarnList(id, type, level, begin,end);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", list);
    return map;
  }



}
